package com.ruoyi.upload.service.impl;

import com.ruoyi.common.entity.uploadentity.Upload;
import com.ruoyi.common.utils.LogUtils;
import com.ruoyi.common.utils.file.FileTypeUtils;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.common.utils.file.FileUtils;
import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.upload.config.Constant;
import com.ruoyi.upload.service.UploadService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;

@Service
public class UploadServiceImpl implements UploadService{

    @Autowired
    private Constant constant;

    /**
     * 上传文件
     * @param multipartFile
     * @return Upload
     */
    @Override
    public Upload upLoad(MultipartFile multipartFile) {
        Upload upload = new Upload();
        if (!multipartFile.isEmpty()){
            try {
                //上传路径
                String filePath = constant.getTEMPLATE_PROFILE();
                String fileName  = FileUploadUtils.upload(filePath,multipartFile);
                String fileType = FileTypeUtils.getFileType(multipartFile.getOriginalFilename());
                /**判断文件类型 */
                if (!fileType.equals("doc")){
                    upload.setResultMsg("文件类型错误！");
                    upload.setSuccess(false);
                    return upload;
                }
                String splitFileName = splitFileName(fileName);
                upload.setFileName(splitFileName);
                upload.setFileType(fileType);
                upload.setId(IdUtils.simpleUUID());
                upload.setSuccess(true);
                upload.setResultMsg("上传成功！");
                return upload;
            }catch (Exception e){
                LogUtils.getBlock(e);
                e.printStackTrace();
                upload.setResultMsg("上传失败!");
                upload.setSuccess(false);
                return upload;
            }
        }else {
            upload.setResultMsg("请选择附件!");
            upload.setSuccess(false);
            return upload;
        }
    }

    /**
     * 文件批量上传
     * @param multipartFile
     * @return
     */
    @Override
    public List<Upload> upLoad(MultipartFile[] multipartFile) throws Exception{
        List<Upload> uploadList = new ArrayList<>();
        /**判断上传的文件是否为空 */
        for (int i = 0; i < multipartFile.length; i++) {
            Upload upload = new Upload();
            String filePath = constant.getTEMPLATE_PROFILE();
            String fileName  = FileUploadUtils.upload(filePath,multipartFile[i]);
            String fileType = FileTypeUtils.getFileType(multipartFile[i].getOriginalFilename());

            String splitFileName = splitFileName(fileName);
            upload.setFileName(splitFileName);
            upload.setFileType(fileType);
            upload.setId(IdUtils.simpleUUID());
            upload.setSuccess(true);
            upload.setResultMsg("上传成功！");
            uploadList.add(upload);
        }
        return uploadList;
    }

    /**
     * 截取文件名
     * @param name
     * @return
     */
    @Override
    public String splitFileName(String name) {
        String[] split = name.split("/profile/");
        String fileName = split[1];
        return fileName;
    }

    /**
     * 删除文件
     * @param resource
     * @return
     */
    @Override
    public boolean deleteFile(String resource) {
        try {
            FileUtils.deleteFile(resource);
        }catch (Exception e){
            LogUtils.getBlock(e);
            e.printStackTrace();
            return false;
        }
        return true;
    }


}
